package com.baidu.mapsdkexample.uploaddata;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.baidu.mapsdkexample.R;

import java.io.InputStream;
import java.util.List;

import cn.leancloud.AVLogger;
import cn.leancloud.AVOSCloud;
import cn.leancloud.AVObject;
import cn.leancloud.AVQuery;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class DataActivity extends AppCompatActivity {

    private List<PointInfo> listPoint;
    private List<AVObject> listTimebox;//时间矩阵-数据库

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data);

        InputStream is = getResources().openRawResource(R.raw.json);//-------------------------------------------------
        try {
             listPoint= leancloud.getInfosFromJson(is);//解析json到javabean
        }catch(Exception ex){
            ex.printStackTrace();
        }

        AVOSCloud.setLogLevel(AVLogger.Level.DEBUG);
        AVOSCloud.initialize(
                this,
                "gYKKKJUqrlSsLCUDRD1aNRHj-gzGzoHsz",
                "viezs5eeEG4dLaPay20EaEXd",
                "dwxr3lun.api.lncld.net");


        //从数据库获得数据
        AVQuery<AVObject> querytimebox = new AVQuery<>("timebox");
        querytimebox.limit(150);
        querytimebox.orderByAscending("id");//按照id排序，id的数字对应于ViewpointInfo中的景点名称
        querytimebox.findInBackground().subscribe(new Observer<List<AVObject>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<AVObject> avObjects) {
                System.out.println("-----------------------------timebox----" + avObjects.size());
                listTimebox = avObjects;
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                save();
            }
        });
    }

    public void save(){
        for (int i=0;i<listPoint.size();i++) {
            // 构建对象
            AVObject todo = new AVObject("ViewpointInfo");//--------------------------------------------------

            // 为属性赋值
            todo.put("id",listTimebox.get(i+1).getInt("id"));
            todo.put("name",listPoint.get(i).getName() );
            todo.put("lng", listPoint.get(i).getLocation().getLng());
            todo.put("lat",listPoint.get(i).getLocation().getLat());
            todo.put("address",listPoint.get(i).getAddress());
            todo.put("province",listPoint.get(i).getProvince());
            todo.put("city", listPoint.get(i).getCity());
            todo.put("area", listPoint.get(i).getArea());
            todo.put("street_id", listPoint.get(i).getStreet_id());
            todo.put("telephone", listPoint.get(i).getTelephone());
            todo.put("uid",listPoint.get(i).getUid());
            todo.put("tag", listPoint.get(i).getDetail_info().getTag());
            todo.put("type", listPoint.get(i).getDetail_info().getType());
            todo.put("detail_url", listPoint.get(i).getDetail_info().getDetail_url());
            todo.put("overall_rating", listPoint.get(i).getDetail_info().getOverall_rating());
            todo.put("comment_num", listPoint.get(i).getDetail_info().getComment_num());

            // 将对象保存到云端
            todo.saveInBackground().subscribe(new Observer<AVObject>() {
                public void onSubscribe(Disposable disposable) {
                }

                public void onNext(AVObject todo) {
                    // 成功保存之后，执行其他逻辑
                    Log.d("temp", "保存成功。objectId：" + todo.getObjectId());
                }

                public void onError(Throwable throwable) {
                    Log.d("temperror",todo.getString("name"));
                    // 异常处理
                }

                public void onComplete() {
                }
            });

            try {
                Thread.currentThread().sleep(50);//设置主线程延迟，给后台存储留间隙
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        for (int i=0;i<listPoint.size();i++)
        {
            Log.d("listPoint  ", listPoint.get(i).getName()+" "
                    +listPoint.get(i).getAddress()+" tag= "
                    +listPoint.get(i).getTelephone()+" "
                    +listPoint.get(i).getDetail_info().getTag()+" "
                    +listPoint.get(i).getLocation().getLng()+" "
                    +listPoint.get(i).getLocation().getLat()+" "
                    +listPoint.get(i).getDetail_info().getDetail_url()+" "
            );
        }
    }
}